package com.franklin.java.completion.validation;

import com.franklin.java.completion.AbstractJavaDataCompletionContributor;
import com.franklin.java.utils.CompletionUtils;
import com.franklin.java.utils.PsiUtils;
import com.intellij.codeInsight.completion.CompletionParameters;
import com.intellij.codeInsight.lookup.LookupElement;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiWhiteSpace;
import org.apache.commons.lang.ArrayUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 参数校验
 * @author Franklin
 * @since 2021/8/5 11:27
 */
public class JavaDataValidationCompletionContributor extends AbstractJavaDataCompletionContributor {

    @Override
    public List<LookupElement> getLookupElements(CompletionParameters parameters) {
        int offset = parameters.getOffset();
        PsiFile psiFile = parameters.getOriginalFile();
        PsiElement elementAt = psiFile.findElementAt(offset);
        String whiteSpace = "";
        if (Objects.nonNull(elementAt) && elementAt instanceof PsiWhiteSpace){
            PsiWhiteSpace psiWhiteSpace = (PsiWhiteSpace) elementAt;
            String[] split = psiWhiteSpace.getText().split("\n");
            if (!ArrayUtils.isEmpty(split)){
                whiteSpace = split[0];
                if (split.length > 1){
                    whiteSpace = split[1];
                }
            }
        }
        List<LookupElement> results = new ArrayList<>();
        results.add(CompletionUtils.getLookElement("notnull","",fillWhiteSpace("if(StringUtilFly.isNotEmpty($EXPR$)){\n#WHITE#\t\n#WHITE#}",whiteSpace),"EXPR","  check if is not empty",whiteSpace.length() + 2));
        results.add(CompletionUtils.getLookElement("isnull","",fillWhiteSpace("if(StringUtilFly.isEmpty($EXPR$)){\n#WHITE#\t\n#WHITE#}",whiteSpace),"EXPR","  check if is empty",whiteSpace.length() + 2));
        results.add(CompletionUtils.getLookElement("colnull","",fillWhiteSpace("if(CollectionUtils.isEmpty($EXPR$)){\n#WHITE#\t\n#WHITE#}",whiteSpace),"EXPR","  check if is empty collection",whiteSpace.length() + 2));
        results.add(CompletionUtils.getLookElement("colnotnull","",fillWhiteSpace("if(!CollectionUtils.isEmpty($EXPR$)){\n#WHITE#\t\n#WHITE#}",whiteSpace),"EXPR","  check if is empty collection",whiteSpace.length() + 2));
        return results;
    }

}
